//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FractionalDividend.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/Dividend.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (Double rate, Double nominal, DateTime date) : CDividend(CFractionalDividend::typeid)
{
    try
    {
#ifdef HANDLE
        _phFractionalDividend = NULL;
#endif
        QuantLib::Real _rate = (QuantLib::Real)ValueHelper::Convert (rate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
        _ppFractionalDividend = new boost::shared_ptr<QuantLib::FractionalDividend> (new QuantLib::FractionalDividend ( _rate,  _nominal,  _date ));
        SetDividend (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (Double rate, DateTime date) : CDividend(CFractionalDividend::typeid)
{
    try
    {
#ifdef HANDLE
        _phFractionalDividend = NULL;
#endif
        QuantLib::Real _rate = (QuantLib::Real)ValueHelper::Convert (rate);
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
        _ppFractionalDividend = new boost::shared_ptr<QuantLib::FractionalDividend> (new QuantLib::FractionalDividend ( _rate,  _date ));
        SetDividend (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (boost::shared_ptr<QuantLib::FractionalDividend>& childNative, Object^ owner) : CDividend(CFractionalDividend::typeid)
{
#ifdef HANDLE
	_phFractionalDividend = NULL;
#endif
	_ppFractionalDividend = &childNative;
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
}
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (QuantLib::FractionalDividend& childNative, Object^ owner) : CDividend(CFractionalDividend::typeid)
{
#ifdef HANDLE
	_phFractionalDividend = NULL;
#endif
	_ppFractionalDividend = new boost::shared_ptr<QuantLib::FractionalDividend> (&childNative);
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
    _FractionalDividendOwner = owner;
    _DividendOwner = owner;
}

Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (CFractionalDividend^ copy) : CDividend(CFractionalDividend::typeid)
{
#ifdef HANDLE
	_phFractionalDividend = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFractionalDividend = new boost::shared_ptr<QuantLib::FractionalDividend> (copy->GetShared());
        _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
    }
}
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (System::Type^ t) : CDividend(CFractionalDividend::typeid)
{
#ifdef HANDLE
	_phFractionalDividend = NULL;
#endif
	if (!t->IsSubclassOf(CFractionalDividend::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (QuantLib::Handle<QuantLib::FractionalDividend>& childNative, Object^ owner)  : CDividend(CFractionalDividend::typeid)
{
	_phFractionalDividend = &childNative;
	_ppFractionalDividend = &static_cast<boost::shared_ptr<QuantLib::FractionalDividend>>(childNative.currentLink());
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
    _FractionalDividendOwner = owner;
}
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (QuantLib::Handle<QuantLib::FractionalDividend> childNative)  : CDividend(CFractionalDividend::typeid)
{
	_phFractionalDividend = &childNative;
	_ppFractionalDividend = &static_cast<boost::shared_ptr<QuantLib::FractionalDividend>>(childNative.currentLink());
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CFractionalDividend::CFractionalDividend (QuantLib::FractionalDividend childNative)  : CDividend(CFractionalDividend::typeid)
{
#ifdef HANDLE
	_phFractionalDividend = NULL;
#endif
	_ppFractionalDividend = new boost::shared_ptr<QuantLib::FractionalDividend> (new QuantLib::FractionalDividend (childNative));
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFractionalDividend));
}
#endif

Cephei::QL::Cashflows::CFractionalDividend::~CFractionalDividend ()
{
    if (_ppFractionalDividend != NULL)
    {
	    delete _ppFractionalDividend;
        _ppFractionalDividend = NULL;
    }
}
Cephei::QL::Cashflows::CFractionalDividend::!CFractionalDividend ()
{
    if (_ppFractionalDividend != NULL)
    {
	    delete _ppFractionalDividend;
    }
}
QuantLib::FractionalDividend& Cephei::QL::Cashflows::CFractionalDividend::GetReference ()
{
    if (_ppFractionalDividend == NULL) throw gcnew NativeNullException ();
	return **_ppFractionalDividend;
}
boost::shared_ptr<QuantLib::FractionalDividend>& Cephei::QL::Cashflows::CFractionalDividend::GetShared ()
{
    if (_ppFractionalDividend == NULL) throw gcnew NativeNullException ();
	return *_ppFractionalDividend;
}
QuantLib::FractionalDividend* Cephei::QL::Cashflows::CFractionalDividend::GetPointer ()
{
    if (_ppFractionalDividend == NULL) throw gcnew NativeNullException ();
	return &**_ppFractionalDividend;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FractionalDividend>& Cephei::QL::Cashflows::CFractionalDividend::GetHandle ()
{
	if (_phFractionalDividend == NULL)
	{
		_phFractionalDividend = new Handle<QuantLib::FractionalDividend> (*_ppFractionalDividend);
	}
	return *_phFractionalDividend;
}
#endif
bool Cephei::QL::Cashflows::CFractionalDividend::HasNative () 
{
	return (_ppFractionalDividend != NULL);
}

Double Cephei::QL::Cashflows::CFractionalDividend::Amount (Double underlying)
{
    try
    {
        QuantLib::Real _underlying = (QuantLib::Real)ValueHelper::Convert (underlying);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFractionalDividend)->amount ( _underlying );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFractionalDividend::Amount ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFractionalDividend)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFractionalDividend::Nominal::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFractionalDividend)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFractionalDividend::Rate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFractionalDividend)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IFractionalDividend^ Cephei::QL::Cashflows::CFractionalDividend_Factory::Create (Double rate, Double nominal, DateTime date)
{
    return gcnew CFractionalDividend ( rate,  nominal,  date);
}
Cephei::QL::Cashflows::IFractionalDividend^ Cephei::QL::Cashflows::CFractionalDividend_Factory::Create (Double rate, DateTime date)
{
    return gcnew CFractionalDividend ( rate,  date);
}
